| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" | 
|  | 2 | "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> | 
|  | 3 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> | 
|  | 4 | <head> | 
|  | 5 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | 
| Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 6 | <meta name="generator" content="AsciiDoc 8.2.5" /> | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 7 | <style type="text/css"> | 
|  | 8 | /* Debug borders */ | 
|  | 9 | p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 { | 
|  | 10 | /* | 
|  | 11 | border: 1px solid red; | 
|  | 12 | */ | 
|  | 13 | } | 
|  | 14 |  | 
|  | 15 | body { | 
|  | 16 | margin: 1em 5% 1em 5%; | 
|  | 17 | } | 
|  | 18 |  | 
| Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 19 | a { | 
|  | 20 | color: blue; | 
|  | 21 | text-decoration: underline; | 
|  | 22 | } | 
|  | 23 | a:visited { | 
|  | 24 | color: fuchsia; | 
|  | 25 | } | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 26 |  | 
|  | 27 | em { | 
|  | 28 | font-style: italic; | 
|  | 29 | } | 
|  | 30 |  | 
|  | 31 | strong { | 
|  | 32 | font-weight: bold; | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | tt { | 
|  | 36 | color: navy; | 
|  | 37 | } | 
|  | 38 |  | 
|  | 39 | h1, h2, h3, h4, h5, h6 { | 
|  | 40 | color: #527bbd; | 
|  | 41 | font-family: sans-serif; | 
|  | 42 | margin-top: 1.2em; | 
|  | 43 | margin-bottom: 0.5em; | 
|  | 44 | line-height: 1.3; | 
|  | 45 | } | 
|  | 46 |  | 
| Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 47 | h1, h2, h3 { | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 48 | border-bottom: 2px solid silver; | 
|  | 49 | } | 
|  | 50 | h2 { | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 51 | padding-top: 0.5em; | 
|  | 52 | } | 
| Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 53 | h3 { | 
|  | 54 | float: left; | 
|  | 55 | } | 
|  | 56 | h3 + * { | 
|  | 57 | clear: left; | 
|  | 58 | } | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 59 |  | 
|  | 60 | div.sectionbody { | 
|  | 61 | font-family: serif; | 
|  | 62 | margin-left: 0; | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | hr { | 
|  | 66 | border: 1px solid silver; | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | p { | 
|  | 70 | margin-top: 0.5em; | 
|  | 71 | margin-bottom: 0.5em; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | pre { | 
|  | 75 | padding: 0; | 
|  | 76 | margin: 0; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | span#author { | 
|  | 80 | color: #527bbd; | 
|  | 81 | font-family: sans-serif; | 
|  | 82 | font-weight: bold; | 
| Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 83 | font-size: 1.1em; | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 84 | } | 
|  | 85 | span#email { | 
|  | 86 | } | 
|  | 87 | span#revision { | 
|  | 88 | font-family: sans-serif; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | div#footer { | 
|  | 92 | font-family: sans-serif; | 
|  | 93 | font-size: small; | 
|  | 94 | border-top: 2px solid silver; | 
|  | 95 | padding-top: 0.5em; | 
|  | 96 | margin-top: 4.0em; | 
|  | 97 | } | 
|  | 98 | div#footer-text { | 
|  | 99 | float: left; | 
|  | 100 | padding-bottom: 0.5em; | 
|  | 101 | } | 
|  | 102 | div#footer-badges { | 
|  | 103 | float: right; | 
|  | 104 | padding-bottom: 0.5em; | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | div#preamble, | 
|  | 108 | div.tableblock, div.imageblock, div.exampleblock, div.verseblock, | 
|  | 109 | div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock, | 
|  | 110 | div.admonitionblock { | 
|  | 111 | margin-right: 10%; | 
|  | 112 | margin-top: 1.5em; | 
|  | 113 | margin-bottom: 1.5em; | 
|  | 114 | } | 
|  | 115 | div.admonitionblock { | 
|  | 116 | margin-top: 2.5em; | 
|  | 117 | margin-bottom: 2.5em; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | div.content { /* Block element content. */ | 
|  | 121 | padding: 0; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | /* Block element titles. */ | 
|  | 125 | div.title, caption.title { | 
|  | 126 | font-family: sans-serif; | 
|  | 127 | font-weight: bold; | 
|  | 128 | text-align: left; | 
|  | 129 | margin-top: 1.0em; | 
|  | 130 | margin-bottom: 0.5em; | 
|  | 131 | } | 
|  | 132 | div.title + * { | 
|  | 133 | margin-top: 0; | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | td div.title:first-child { | 
|  | 137 | margin-top: 0.0em; | 
|  | 138 | } | 
|  | 139 | div.content div.title:first-child { | 
|  | 140 | margin-top: 0.0em; | 
|  | 141 | } | 
|  | 142 | div.content + div.title { | 
|  | 143 | margin-top: 0.0em; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | div.sidebarblock > div.content { | 
|  | 147 | background: #ffffee; | 
|  | 148 | border: 1px solid silver; | 
|  | 149 | padding: 0.5em; | 
|  | 150 | } | 
|  | 151 |  | 
| Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 152 | div.listingblock { | 
|  | 153 | margin-right: 0%; | 
|  | 154 | } | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 155 | div.listingblock > div.content { | 
|  | 156 | border: 1px solid silver; | 
|  | 157 | background: #f4f4f4; | 
|  | 158 | padding: 0.5em; | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | div.quoteblock > div.content { | 
|  | 162 | padding-left: 2.0em; | 
|  | 163 | } | 
| Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 164 |  | 
|  | 165 | div.attribution { | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 166 | text-align: right; | 
|  | 167 | } | 
| Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 168 | div.verseblock + div.attribution { | 
|  | 169 | text-align: left; | 
|  | 170 | } | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 171 |  | 
|  | 172 | div.admonitionblock .icon { | 
|  | 173 | vertical-align: top; | 
|  | 174 | font-size: 1.1em; | 
|  | 175 | font-weight: bold; | 
|  | 176 | text-decoration: underline; | 
|  | 177 | color: #527bbd; | 
|  | 178 | padding-right: 0.5em; | 
|  | 179 | } | 
|  | 180 | div.admonitionblock td.content { | 
|  | 181 | padding-left: 0.5em; | 
|  | 182 | border-left: 2px solid silver; | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | div.exampleblock > div.content { | 
|  | 186 | border-left: 2px solid silver; | 
|  | 187 | padding: 0.5em; | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | div.verseblock div.content { | 
|  | 191 | white-space: pre; | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | div.imageblock div.content { padding-left: 0; } | 
|  | 195 | div.imageblock img { border: 1px solid silver; } | 
|  | 196 | span.image img { border-style: none; } | 
|  | 197 |  | 
|  | 198 | dl { | 
|  | 199 | margin-top: 0.8em; | 
|  | 200 | margin-bottom: 0.8em; | 
|  | 201 | } | 
|  | 202 | dt { | 
|  | 203 | margin-top: 0.5em; | 
|  | 204 | margin-bottom: 0; | 
|  | 205 | font-style: italic; | 
|  | 206 | } | 
|  | 207 | dd > *:first-child { | 
|  | 208 | margin-top: 0; | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | ul, ol { | 
|  | 212 | list-style-position: outside; | 
|  | 213 | } | 
| Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 214 | div.olist2 ol { | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 215 | list-style-type: lower-alpha; | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | div.tableblock > table { | 
| Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 219 | border: 3px solid #527bbd; | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 220 | } | 
|  | 221 | thead { | 
|  | 222 | font-family: sans-serif; | 
|  | 223 | font-weight: bold; | 
|  | 224 | } | 
|  | 225 | tfoot { | 
|  | 226 | font-weight: bold; | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | div.hlist { | 
|  | 230 | margin-top: 0.8em; | 
|  | 231 | margin-bottom: 0.8em; | 
|  | 232 | } | 
| Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 233 | div.hlist td { | 
|  | 234 | padding-bottom: 5px; | 
|  | 235 | } | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 236 | td.hlist1 { | 
|  | 237 | vertical-align: top; | 
|  | 238 | font-style: italic; | 
|  | 239 | padding-right: 0.8em; | 
|  | 240 | } | 
|  | 241 | td.hlist2 { | 
|  | 242 | vertical-align: top; | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | @media print { | 
|  | 246 | div#footer-badges { display: none; } | 
|  | 247 | } | 
| Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 248 |  | 
|  | 249 | div#toctitle { | 
|  | 250 | color: #527bbd; | 
|  | 251 | font-family: sans-serif; | 
|  | 252 | font-size: 1.1em; | 
|  | 253 | font-weight: bold; | 
|  | 254 | margin-top: 1.0em; | 
|  | 255 | margin-bottom: 0.1em; | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 { | 
|  | 259 | margin-top: 0; | 
|  | 260 | margin-bottom: 0; | 
|  | 261 | } | 
|  | 262 | div.toclevel2 { | 
|  | 263 | margin-left: 2em; | 
|  | 264 | font-size: 0.9em; | 
|  | 265 | } | 
|  | 266 | div.toclevel3 { | 
|  | 267 | margin-left: 4em; | 
|  | 268 | font-size: 0.9em; | 
|  | 269 | } | 
|  | 270 | div.toclevel4 { | 
|  | 271 | margin-left: 6em; | 
|  | 272 | font-size: 0.9em; | 
|  | 273 | } | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 274 | /* Workarounds for IE6's broken and incomplete CSS2. */ | 
|  | 275 |  | 
|  | 276 | div.sidebar-content { | 
|  | 277 | background: #ffffee; | 
|  | 278 | border: 1px solid silver; | 
|  | 279 | padding: 0.5em; | 
|  | 280 | } | 
|  | 281 | div.sidebar-title, div.image-title { | 
|  | 282 | font-family: sans-serif; | 
|  | 283 | font-weight: bold; | 
|  | 284 | margin-top: 0.0em; | 
|  | 285 | margin-bottom: 0.5em; | 
|  | 286 | } | 
|  | 287 |  | 
|  | 288 | div.listingblock div.content { | 
|  | 289 | border: 1px solid silver; | 
|  | 290 | background: #f4f4f4; | 
|  | 291 | padding: 0.5em; | 
|  | 292 | } | 
|  | 293 |  | 
|  | 294 | div.quoteblock-content { | 
|  | 295 | padding-left: 2.0em; | 
|  | 296 | } | 
|  | 297 |  | 
|  | 298 | div.exampleblock-content { | 
|  | 299 | border-left: 2px solid silver; | 
|  | 300 | padding-left: 0.5em; | 
|  | 301 | } | 
| Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 302 |  | 
|  | 303 | /* IE6 sets dynamically generated links as visited. */ | 
|  | 304 | div#toc a:visited { color: blue; } | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 305 | </style> | 
|  | 306 | <title>allocation growing API</title> | 
|  | 307 | </head> | 
|  | 308 | <body> | 
|  | 309 | <div id="header"> | 
|  | 310 | <h1>allocation growing API</h1> | 
|  | 311 | </div> | 
|  | 312 | <div id="preamble"> | 
|  | 313 | <div class="sectionbody"> | 
| Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 314 | <div class="para"><p>Dynamically growing an array using realloc() is error prone and boring.</p></div> | 
|  | 315 | <div class="para"><p>Define your array with:</p></div> | 
|  | 316 | <div class="ilist"><ul> | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 317 | <li> | 
|  | 318 | <p> | 
|  | 319 | a pointer (<tt>ary</tt>) that points at the array, initialized to <tt>NULL</tt>; | 
|  | 320 | </p> | 
|  | 321 | </li> | 
|  | 322 | <li> | 
|  | 323 | <p> | 
|  | 324 | an integer variable (<tt>alloc</tt>) that keeps track of how big the current | 
|  | 325 | allocation is, initialized to <tt>0</tt>; | 
|  | 326 | </p> | 
|  | 327 | </li> | 
|  | 328 | <li> | 
|  | 329 | <p> | 
|  | 330 | another integer variable (<tt>nr</tt>) to keep track of how many elements the | 
|  | 331 | array currently has, initialized to <tt>0</tt>. | 
|  | 332 | </p> | 
|  | 333 | </li> | 
| Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 334 | </ul></div> | 
|  | 335 | <div class="para"><p>Then before adding <tt>n`th element to the array, call `ALLOC_GROW(ary, n, | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 336 | alloc)</tt>. This ensures that the array can hold at least <tt>n</tt> elements by | 
| Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 337 | calling <tt>realloc(3)</tt> and adjusting <tt>alloc</tt> variable.</p></div> | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 338 | <div class="listingblock"> | 
|  | 339 | <div class="content"> | 
|  | 340 | <pre><tt>sometype *ary; | 
|  | 341 | size_t nr; | 
|  | 342 | size_t alloc | 
|  | 343 |  | 
|  | 344 | for (i = 0; i < nr; i++) | 
|  | 345 | if (we like ary[i] already) | 
|  | 346 | return; | 
|  | 347 |  | 
|  | 348 | /* we did not like any existing one, so add one */ | 
|  | 349 | ALLOC_GROW(ary, nr + 1, alloc); | 
|  | 350 | ary[nr++] = value you like;</tt></pre> | 
|  | 351 | </div></div> | 
| Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 352 | <div class="para"><p>You are responsible for updating the <tt>nr</tt> variable.</p></div> | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 353 | </div> | 
|  | 354 | </div> | 
|  | 355 | <div id="footer"> | 
|  | 356 | <div id="footer-text"> | 
| Junio C Hamano | ec87f52 | 2008-12-10 08:35:25 | [diff] [blame] | 357 | Last updated 2008-12-10 08:33:36 UTC | 
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 358 | </div> | 
|  | 359 | </div> | 
|  | 360 | </body> | 
|  | 361 | </html> |